<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
/*
	微站列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return=array();
if($_RQ['op']=='get'){
	//模板设计
	$id=intval($_RQ['id']);
	$web_id=intval($_RQ['web_id']);
	if(!$web_id && $id){
		$web_id=pdo_getcolumn('core_account_wxapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'web_id');
		$web_id=pdo_getcolumn('core_mobile_web',array('id'=>$web_id,'uniacid'=>$_SESSION['uniacid']),'id');
		if(!$web_id){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('syscolor','title','web_id'));
			$insert['uniacid']=$_SESSION['uniacid'];
			$insert['title']=$wxapp['title'];
			$insert['syscolor']=$wxapp['syscolor'];
			$insert['wxapp_id']=$id;
			$insert['createtime']=time();
			pdo_insert('core_mobile_web',$insert);
			$web_id=pdo_insertid();
			pdo_update('core_account_wxapp',array('web_id'=>$web_id),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	$_SESSION['mobile_web']['wxapp_id']=$id;
	$_SESSION['mobile_web']['web_id']=$web_id;
	if(!$web_id){
		exi('请选择小程序','error');
	}
	$app_type=pdo_getcolumn('core_account_wxapp',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'type');
	$tpls=pdo_getall('core_account_web_tpl',array('web_id'=>$web_id,'tpl_id'=>0,'uniacid'=>$_SESSION['uniacid']));
	if(!empty($tpls)){
		foreach($tpls as &$item){
			$info=pdo_get('core_mobile_template',array('id'=>$item['template_id']),array('image','title','plugin_name','pageaction'));
			$item['image']=$info['image'];
			$item['title']=$info['title'];
		}
	}else{
		$tpldata=pdo_get('core_account_wxapp',array('id'=>$web_id,'uniacid'=>$_SESSION['uniacid']),array('items','setting','title','plugin_name'));
		$tpldata['uniacid']=$_SESSION['uniacid'];
		$tpldata['createtime']=TIMESTAMP;
		$tpldata['web_id']=$web_id;
		$tpldata['is_open']=1;
		pdo_insert('core_account_web_tpl',$tpldata);
		$tpl_id=pdo_insertid();
		$pages=pdo_getall('core_mobile_web',array('webid'=>$web_id,'uniacid'=>$_SESSION['uniacid']),array('items','setting','title','plugin_name','pageaction','html'));
		foreach($pages as &$pv){
			$pv['uniacid']=$_SESSION['uniacid'];
			$pv['createtime']=TIMESTAMP;
			$pv['tpl_id']=$tpl_id;
			$pv['web_id']=$web_id;
			pdo_insert('core_account_web_tpl',$pv);
		}
		exi('初始化模板成功！','','/wxapp.app_web_tpls/get/id='.$id);
	}
	$return['tpls']=$tpls;
	$return['listurl']=$app_type==8?'/wxapp.list/list/':'/wxapp.plugin/list/';
	exi($return);
}
if($_RQ['op']=='add'){
	$id=intval($_RQ['id']);
	$web_id=$_SESSION['mobile_web']['web_id'];
	if($id){
		//判断是否免费、是否购买
		//读取数据
		$data=pdo_get('core_mobile_template',array('id'=>$id),array('items','setting','title','pages','plugin_name','pageaction'));
		$pages= $data['pages']?json_decode($data['pages'],true):array();
		$insert['items']=$data['items'];
		$insert['setting']=$data['setting'];
		$insert['plugin_name']=$data['plugin_name'];
		$insert['pageaction']=$data['pageaction'];
	}
	$tpldata=array(
		'uniacid'=>$_SESSION['uniacid'],
		'createtime'=>TIMESTAMP,
		'web_id'=>$web_id,
		'is_open'=>1,
		'template_id'=>$id,
		'items'=>$insert['items'],
		'setting'=>$insert['setting'],
		'plugin_name'=>$insert['plugin_name'],
		'pageaction'=>$insert['pageaction'],
		'pages'=>$data['pages'],
	);
	pdo_update('core_account_web_tpl',array('is_open'=>0),array('uniacid'=>$_SESSION['uniacid'],'web_id'=>$web_id));
	pdo_insert('core_account_web_tpl',$tpldata);
	$tpl_id=pdo_insertid();
	if(!empty($pages)){
		foreach($pages as &$row){
			$row['uniacid']=$_SESSION['uniacid'];
			$row['tpl_id']=$tpl_id;
			$row['createtime']=TIMESTAMP;
			pdo_insert('core_account_web_tpl',$row);
		}
	}
	$action=array('action'=>'wxapp.list','op'=>'design','query'=>'id='.$tpl_id);
	if($id){
		$message="使用模版[{$data['title']}]创建小程序模板成功，准备进入详情设置页！";
	}else{
		$message="直接创建小程序模板成功，准备进入小程序详情设置页！";
	}
	exi($message,'success',$action);
}
if($_RQ['op']=='open'){
	$id=intval($_RQ['id']);
	$web_id=$_SESSION['mobile_web']['web_id'];
	pdo_update('core_account_web_tpl',array('is_open'=>0),array('uniacid'=>$_SESSION['uniacid'],'web_id'=>$web_id));
	pdo_update('core_account_web_tpl',array('is_open'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('启用成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	$web_id=$_SESSION['mobile_web']['web_id'];
	if($id){
		pdo_delete('core_account_web_tpl',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('删除成功！','','/website.mobile_web_tpls/get/id='.$web_id);
}